//
//  PaletteView.m
//
//  Created by wangzhi on 16/02/22.
//  Copyright (c) 2016年 王志. All rights reserved.
//

#import "PaletteView.h"
#import "SaveOld.h"

@interface PaletteView ()

@end

@implementation PaletteView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.colorLine = [UIColor redColor]; // 设置线的颜色
        self.widthLine = 3; // 设置线的宽度
    }
    return self;
}

- (void)setColorLine:(UIColor *)colorLine {
    _colorLine = colorLine;
    [self setNeedsDisplay];
}

- (void)setWidthLine:(NSInteger)widthLine {
    _widthLine = widthLine;
    [self setNeedsDisplay];
}

- (NSMutableArray *)movePoints {
    if (_movePoints == nil) {
        _movePoints = [[NSMutableArray alloc] init];
    }
    return _movePoints;
}

//- (NSMutableArray *)allPointsArr {
//    if (_allPointsArr == nil) {
//        _allPointsArr = [[NSMutableArray alloc] init];
//    }
//    return _allPointsArr;
//}

- (void)setModel:(PaletteModel *)model {
    _model = model;
    [self setNeedsDisplay];
}

/**
 *  橡皮
 */
- (void)rubLine {
    self.colorLine = self.backgroundColor;
}

/**
 *  撤销
 */
- (void)revoke {
    [self setNeedsDisplay];
//    [self.allPointsArr removeLastObject];
}

- (void)last {
    if (self.model.curIndex <= 0 ) {
        return;
    }
    self.model.curIndex--;
    
    if (self.model.curIndex < self.model.startIndex) {
        __block NSUInteger curStartIndex = 0;
        [self.model.startPointsArr enumerateObjectsUsingBlock:^(NSNumber *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (self.model.startIndex == [obj integerValue]) {
                curStartIndex = idx;
                *stop = YES;
            }
        }];
        
        if (curStartIndex > 0) {
            NSUInteger startIndex = [self.model.startPointsArr[curStartIndex - 1] integerValue];
            self.model.startIndex = startIndex;
            self.model.curIndex++;
        }

    }
    
    [self setNeedsDisplay];
}

- (void)next {
    if (self.model.curIndex >= self.model.allPointsArr.count) {
        return;
    }
    self.model.curIndex++;
    
    if ([self.model.startPointsArr.lastObject integerValue] != self.model.startIndex) {
        
        __block NSInteger curStartIndex = 0;
        [self.model.startPointsArr enumerateObjectsUsingBlock:^(NSNumber *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj integerValue] == self.model.startIndex) {
                curStartIndex = idx;
                *stop = YES;
            }
        }];
        
        if ([self.model.startPointsArr[curStartIndex + 1] integerValue] < self.model.curIndex) {
            self.model.startIndex = [self.model.startPointsArr[curStartIndex + 1] integerValue];
            self.model.curIndex--;
        }
    }
//    if (self.model.startIndex < self.model.startPointsArr.count - 1) {
//        NSUInteger nextStartIndex = [self.model.startPointsArr[self.model.startIndex + 1] integerValue];
//        if (self.model.curIndex >= nextStartIndex) {
//            self.model.startIndex++;
//        }
//    }
    [self setNeedsDisplay];
}

/**
 *  清屏
 */
- (void)clearScreen {
    if (self.model.curIndex == self.model.startIndex) {
        return;
    }
    
    __block NSUInteger clearIndex = 0;
    [self.model.startPointsArr enumerateObjectsUsingBlock:^(NSNumber *start, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([start integerValue] >= self.model.curIndex) {
            clearIndex = idx;
        }
    }];
    
    if (clearIndex != 0) {
        [self.model.startPointsArr removeObjectsInRange:NSMakeRange(clearIndex , self.model.startPointsArr.count - clearIndex)];
    }
    [self.model.startPointsArr addObject:[NSNumber numberWithInteger:self.model.curIndex]];
    self.model.startIndex = self.model.curIndex;
    
    [self setNeedsDisplay];
//    [self.allPointsArr removeAllObjects];
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (NSInteger i = self.model.startIndex; i < self.model.curIndex; i++) {
        
        NSArray *arr = [self.model.allPointsArr objectAtIndex:i];
        [self.colorLine setStroke];
        CGContextSetLineWidth(context, self.widthLine);
        // 调用画线方法
        [self fromPointToLineWithContext:context withPoingArr:arr];
    }
    
    if (self.movePoints == nil || self.movePoints.count < 2) {
        return;
    }
    [self.colorLine setStroke];
    CGContextSetLineWidth(context, self.widthLine);
    // 调用画线方法
    [self fromPointToLineWithContext:context withPoingArr:self.movePoints];
}

/**
 *  画线
 */
- (void)fromPointToLineWithContext:(CGContextRef)context withPoingArr:(NSArray *)arr {
    for (int i = 0; i < arr.count - 1; i++) {
        CGMutablePathRef path = CGPathCreateMutable();
        
        CGPoint point1 = [arr[i] CGPointValue];
        CGPathMoveToPoint(path, nil, point1.x, point1.y); 
        CGContextSetLineCap(context, kCGLineCapRound);
        
        CGPoint point2 = [arr[i + 1] CGPointValue];
        CGPathAddLineToPoint(path, nil, point2.x, point2.y);
        
        CGContextAddPath(context, path);
        CGContextDrawPath(context, kCGPathStroke);
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    // 取得手指点击的起始点
    CGPoint points = [touch locationInView:self];
    [self.movePoints addObject:[NSValue valueWithCGPoint:points]];
    
    //------------------------------------------
//    SaveOld *saveOld = [[SaveOld alloc] init];
//    saveOld.colorOld = self.colorLine;
//    saveOld.widthOld = self.widthLine;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    // 取得手指点击的起始点
    CGPoint points = [touch locationInView:self];
    [self.movePoints addObject:[NSValue valueWithCGPoint:points]];
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (self.model.allPointsArr.count > 0 && self.model.curIndex < self.model.allPointsArr.count) {
        [self.model.allPointsArr removeObjectsInRange:NSMakeRange(self.model.curIndex , self.model.allPointsArr.count - self.model.curIndex)];
    }
    [self.model.allPointsArr addObject:self.movePoints];
    
    self.model.curIndex++;
    self.movePoints = nil;
}

@end
